package com.me.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.me.pojo.Employee;
import com.me.pojo.Enhance;
import com.me.pojo.MyOrder;
import com.me.pojo.OrderMessage;
import com.me.service.EmployeeService;
import com.me.service.UserService;

@RequestMapping("/employee")
@Controller
public class EmployeeController 
{
	@Autowired
	private EmployeeService employeeService;
	
	@Autowired
	private UserService userService;
	
	public EmployeeService getEmployeeService() {
		return employeeService;
	}

	public void setEmployeeService(EmployeeService employeeService) {
		this.employeeService = employeeService;
	}
	@RequestMapping("/toConsumeLogin")
	public String toConsumeLogin()
	{
		
		return "forward:/WEB-INF/jsp/consumeLogin.jsp";
		
	}
	public ModelAndView employeeLogin(HttpSession session,String username,String password)
	{
		String url=employeeService.login(username, password);
		ModelAndView m=new ModelAndView();
		if("employeeLogin".equals(url))
		{
			m.addObject("login", true);
			m.setViewName(url);
			return m;
		}
		List<Enhance> enhanceList=employeeService.SelectAllEnhance();
		m.addObject("enhanceList", enhanceList);
		Employee employee=employeeService.selectEmployee(username, password);
		session.setAttribute("employeeid", employee.getId());
		session.setAttribute("jurisdiction", employee.getJurisdiction());
		session.setAttribute("name", employee.getName());
		m.setViewName(url);
		return m;
	}
	@RequestMapping("/register")
	public String register(HttpSession session,Model model)
	{
		String jurisdiction=(String) session.getAttribute("jurisdiction");
		if(!("A".equals(jurisdiction)))
		{
			model.addAttribute("jurisdiction", true);
			List<Enhance> enhanceList=employeeService.SelectAllEnhance();
			model.addAttribute("enhanceList", enhanceList);
			return "forward:/WEB-INF/jsp/employeeBase.jsp";
		}
		return "forward:/WEB-INF/jsp/register.jsp";
	}
	
	@RequestMapping("/toEmployeeBase")
	public String toEmployeeBase(Model model)
	{	
		List<Enhance> enhanceList=employeeService.SelectAllEnhance();
		model.addAttribute("enhanceList", enhanceList);
		return "forward:/WEB-INF/jsp/employeeBase.jsp";
	}
	
	@RequestMapping("/toEmployeeOrder")
	public ModelAndView toMyOrder()
	{	
		return getModelAndView();
	}
	
	
	@RequestMapping("/sendOrder")
	public ModelAndView sendOrder(Integer myOrderId,HttpSession session)
	{
		
		String jurisdiction=(String) session.getAttribute("jurisdiction");
		if(!("C".equals(jurisdiction)))
		{
			ModelAndView m=getModelAndView();
			m.addObject("jurisdiction",true);
			return m;
		}
		MyOrder record=new MyOrder();
		record.setId(myOrderId);
		record.setOrderStatusId(3);
		userService.updateMyOrderByPrimaryKeySelective(record);
		return getModelAndView();
	}
	
	@RequestMapping("/orderDetails")
	public String orderDetails(String code,Model model)
	{
		Integer orderId=employeeService.selectIdBycode(code);
		List<Enhance> enhanceList=employeeService.selectEnhanceByOrderId(orderId);
		model.addAttribute("enhanceList", enhanceList);
		model.addAttribute("code", code);
		return "forward:/WEB-INF/jsp/employeeOrderDetails.jsp";
	}
//	
//	@RequestMapping("/payOrder")
//	public ModelAndView payOrder(HttpSession session,Integer myOrderId)
//	{
//		MyOrder record=new MyOrder();
//		record.setId(myOrderId);
//		record.setOrderStatusId(2);
//		userService.updateMyOrderByPrimaryKeySelective(record);
//		return getModelAndView(session);
//	}
//	
	public ModelAndView getModelAndView()
	{
		ModelAndView m =new ModelAndView();
		List<Enhance> enhanceList=employeeService.selectOrder();
		m.addObject("enhanceList", enhanceList);
		m.setViewName("employeeOrder");
		return m;
	}
	
	@RequestMapping("/recallOrder")
	public ModelAndView recallOrder(HttpSession session,Integer myOrderId)
	{
		String jurisdiction=(String) session.getAttribute("jurisdiction");
		if(!"D".equals(jurisdiction))
		{
			ModelAndView m=getModelAndView();
			m.addObject("jurisdiction",true);
			return m;
		}
		MyOrder record=new MyOrder();
		record.setId(myOrderId);
		record.setOrderStatusId(5);
		userService.updateMyOrderByPrimaryKeySelective(record);
		return getModelAndView();
	}
	
	@RequestMapping("/toEmployeeMessage")
	public ModelAndView toEmployeeMessage(HttpSession session)
	{	
		String jurisdiction=(String) session.getAttribute("jurisdiction");
		ModelAndView m=new ModelAndView();
		if(!"E".equals(jurisdiction))
		{
			List<Enhance> enhanceList=employeeService.SelectAllEnhance();
			m.addObject("enhanceList", enhanceList);
			m.addObject("jurisdiction",true);
			m.setViewName("employeeBase");
			return m;
		}
		List<Employee> employeeList=employeeService.selectAll();
		m.addObject("employeeList", employeeList);
//		m.setViewName("alterEmployeeMessage");
		m.setViewName("EmployeeMessage");
		return m;
	}
	
	@RequestMapping("/toAlertEmployeeMessage")
	public String toAlertEmployeeMessage(Integer id,Model model)
	{	
		model.addAttribute("id", id);
		return "forward:/WEB-INF/jsp/alterEmployeeMessage.jsp";
	}
	@RequestMapping("/alterEmployeeMessage")
	public String alertEmployeeMessage(Employee employee,Model m)
	{	
		employeeService.updateById(employee);
		List<Employee> employeeList=employeeService.selectAll();
		m.addAttribute("employeeList", employeeList);
		return "forward:/WEB-INF/jsp/EmployeeMessage.jsp";
	}
}
